﻿using AutoMapper;
using Menus.Dtos;
using Menus.Models;
using Menus.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Menus.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MenusController : ControllerBase
    {
        public  IUserMenusRepository _userMenusRepository;
        public IMapper _mapper;
        public MenusController(IUserMenusRepository userMenusRepository,IMapper mapper) 
        {
            _userMenusRepository = userMenusRepository;
            _mapper = mapper;
        }
        [HttpGet]
        [Authorize]
        public async Task<ReturnJson> GetUserMenu() 
        {
            var role = User.FindFirst(d => d.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role")?.Value;
            
            var Menu = await _userMenusRepository.GetUserMenusAsync(_userMenusRepository.GetPerMissionByToken(role));
            return new ReturnJson { 
            Message="请求成功",
            Status=200,
            Data=Menu
            };
        }
        [HttpOptions]
        public IActionResult Options()
        {
            return Ok();
        }
        [HttpOptions("list")]
        public IActionResult Optionslist()
        {
            return Ok();
        }
        [HttpGet("list")]
        [Authorize(Roles ="admin")]
        public async Task<ReturnJson> GetMenusPermission() 
        {
            IEnumerable<UserMenu> userMenus = await _userMenusRepository.GetUserMenusAsync(0);
           IEnumerable<obtainMenusPermissionDto> obtainMenusPermissionDtos= _mapper.Map<IEnumerable<obtainMenusPermissionDto>>(userMenus);
            IEnumerable<UserSecondMenu> userSecondMenus = await _userMenusRepository.MenuSecondMenusChange0btainMenusPermissionDtoAsync(userMenus);
          List<obtainMenusPermissionDto>list=obtainMenusPermissionDtos.ToList();
            list.AddRange((_mapper.Map<IEnumerable<obtainMenusPermissionDto>>(userSecondMenus)));
            return new ReturnJson
            {
                Message = "请求成功",
                Data = list,
                Status = 200
            };
           
        }
    }
}
